import {isNotEmpty} from "./CommonFunction";

Date.prototype.format = function (fmt) {
  let o = {
    "M+": this.getMonth() + 1,                 //月份
    "d+": this.getDate(),                    //日
    "h+": this.getHours(),                   //小时
    "m+": this.getMinutes(),                 //分
    "s+": this.getSeconds(),                 //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds()             //毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
}
Date.prototype.addYears = function (years) {
  let m = this.getMonth();
  this.setFullYear(this.getFullYear() + years);
  if (m < this.getMonth()) {
    this.setDate(0);
  }
  return this;
}

Date.prototype.addMonths = function (months) {
  let d = this.getDate();
  this.setMonth(this.getMonth() + months);
  if (this.getDate() < d) {
    this.setDate(0);
  }
  return this;
}
Date.prototype.addWeeks = function (weeks) {
  return this.addDays(weeks * 7)
}
Date.prototype.addDays = function (days) {
  return this.addHours(days * 24)
}
Date.prototype.addHours = function (hours) {
  return this.addMinutes(hours * 60)
}
Date.prototype.addMinutes = function (minutes) {
  return this.addSeconds(minutes * 60)
}
Date.prototype.addSeconds = function (seconds) {
  return this.addMilliseconds(seconds * 1000)
}
Date.prototype.addMilliseconds = function (milliseconds) {
  return new Date(this.getTime() + milliseconds)
}

class DateUtil {
  format = function (date, pattern) {
    if (isNotEmpty(date)) {
      let dt = date;
      if (pattern) {
        dt = new Date(date).format(pattern);
      } else {
        dt = new Date(date).format("yyyy-MM-dd hh:mm:ss");
      }
      return dt.toLocaleString();
    } else {
      return null;
    }
  }

  addYears = function (date, years) {
    if (isNotEmpty(date)) {
      return date.addYears(years)
    } else {
      return null;
    }
  }
  addMonths = function (date, months) {
    if (isNotEmpty(date)) {
      return date.addMonths(months)
    } else {
      return null;
    }
  };
  addWeeks = function (date, weeks) {
    if (isNotEmpty(date)) {
      return date.addWeeks(weeks);
    } else {
      return null;
    }
  }
  addDays = function (date, days) {
    if (isNotEmpty(date)) {
      return date.addDays(days);
    } else {
      return null;
    }
  }
  addHours = function (date, hours) {
    if (isNotEmpty(date)) {
      return date.addHours(hours);
    } else {
      return null;
    }
  }
  addMinutes = function (date, minutes) {
    if (isNotEmpty(date)) {
      return date.addMinutes(minutes);
    } else {
      return null;
    }
  }
  addSeconds = function (date, seconds) {
    if (isNotEmpty(date)) {
      return date.addSeconds(seconds)
    } else {
      return null;
    }
  }
  addMilliseconds = function (date, milliseconds) {
    if (isNotEmpty(date)) {
      return date.addMilliseconds(milliseconds);
    } else {
      return null;
    }
  }
}

export default new DateUtil();
